package cn.tedu.sentinel03.controller;




import cn.tedu.sentinel03.service.HelloService;
import cn.tedu.sentinel03.until.Block;
import cn.tedu.sentinel03.until.FallBack;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class HelloController {
    
    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
//    BlockException
    @SentinelResource(value = "sayhi" ,
            blockHandlerClass = Block.class
            ,blockHandler ="blockFunc",
    fallbackClass = FallBack.class,
    fallback = "fallbackFunc" )
    public String sayhi(String name){
        System.out.println(name);
            //处理结果
        String result = helloService.hello(name);
//        //模拟请求无法被处理
        if (true){
           throw new RuntimeException("无法处理请求");
        }
        // 200ms  2000ms
//        try {
//            Thread.sleep(500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return result;
    }
//    限流
//    降级===在请求正常进入controller但是没有办法处理这个请求  让请求降级

    //熔断效果 ： 在方法执行 正常和不正常的情况下  0.5（5/10）  不正常
    // 直接走降级
}
